package com.ruoyi.estate.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.estate.domain.BuildingUnit;
import com.ruoyi.estate.service.IBuildingUnitService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单元管理Controller
 * 
 * @author 博雅
 * @date 2023-12-19
 */
@RestController
@RequestMapping("/estate/unit")
public class BuildingUnitController extends BaseController
{
    @Autowired
    private IBuildingUnitService buildingUnitService;

    /**
     * 查询单元管理列表
     */
    @PreAuthorize("@ss.hasPermi('estate:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(BuildingUnit buildingUnit)
    {
        startPage();
        List<BuildingUnit> list = buildingUnitService.selectBuildingUnitList(buildingUnit);
        return getDataTable(list);
    }

    /**
     * 导出单元管理列表
     */
    @PreAuthorize("@ss.hasPermi('estate:unit:export')")
    @Log(title = "单元管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuildingUnit buildingUnit)
    {
        List<BuildingUnit> list = buildingUnitService.selectBuildingUnitList(buildingUnit);
        ExcelUtil<BuildingUnit> util = new ExcelUtil<BuildingUnit>(BuildingUnit.class);
        util.exportExcel(response, list, "单元管理数据");
    }

    /**
     * 获取单元管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('estate:unit:query')")
    @GetMapping(value = "/{unitId}")
    public AjaxResult getInfo(@PathVariable("unitId") Long unitId)
    {
        return success(buildingUnitService.selectBuildingUnitByUnitId(unitId));
    }

    /**
     * 新增单元管理
     */
    @PreAuthorize("@ss.hasPermi('estate:unit:add')")
    @Log(title = "单元管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuildingUnit buildingUnit)
    {
        return toAjax(buildingUnitService.insertBuildingUnit(buildingUnit));
    }

    /**
     * 修改单元管理
     */
    @PreAuthorize("@ss.hasPermi('estate:unit:edit')")
    @Log(title = "单元管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuildingUnit buildingUnit)
    {
        return toAjax(buildingUnitService.updateBuildingUnit(buildingUnit));
    }

    /**
     * 删除单元管理
     */
    @PreAuthorize("@ss.hasPermi('estate:unit:remove')")
    @Log(title = "单元管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{unitIds}")
    public AjaxResult remove(@PathVariable Long[] unitIds)
    {
        return toAjax(buildingUnitService.deleteBuildingUnitByUnitIds(unitIds));
    }

    //查询楼栋下的单元信息
    @PreAuthorize("@ss.hasPermi('estate:unit:list')")
    @GetMapping("/unitList/{floorId}")
    public AjaxResult unitList(@PathVariable Long floorId)
    {
        BuildingUnit unit=new BuildingUnit();
        unit.setFloorId(floorId);
        return success(buildingUnitService.selectBuildingUnitList(unit));
    }
}
